package com.care.dao.admin.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.care.dao.admin.AdminDao;
import com.care.entity.Doctor;
import com.care.entity.Knowledge;
import com.care.entity.User;


@Repository
public class AdminDaoImpl implements AdminDao{

	@Resource
	private SessionFactory sessionFactory;
	public static void main(String[] args) {
		System.out.println(new AdminDaoImpl().sessionFactory);
	}
	
	@Override
	public User selUser(String name) {
//		User user = this.sessionFactory.getCurrentSession().load(Doctor.class, arg1);
		Query query = this.sessionFactory.getCurrentSession().createQuery("from User where name=? ");
		query.setParameter(0, name);
		User user =(User)query.uniqueResult();
		return user;
	}

	@Override
	public Doctor selDoctor(String name) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Doctor where name=? ");
		query.setParameter(0, name);
		Doctor doctor =(Doctor)query.uniqueResult();
		return doctor;
	}

	@Override
	public Knowledge selKnowledge(String title) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Knowledge where title=? ");
		query.setParameter(0, title);
		Knowledge knowledge =(Knowledge)query.uniqueResult();
		return knowledge;
	}

	@Override
	public boolean addUser(User user) {
		Serializable save = this.sessionFactory.getCurrentSession().save(user);
//		System.out.print(save);
		if(save!=null){
//			System.out.print("true");
			return true;
		}
//		System.out.print("false");
		return false;
//		this.sessionFactory.getCurrentSession().save(user);
//		return true;
	}

	@Override
	public boolean addDoctor(Doctor doctor) {
		Serializable save = this.sessionFactory.getCurrentSession().save(doctor);
		if(save!=null){
			return true;
		}
		return false;
	}
	
	@Override
	public boolean addKnowledge(Knowledge knowledge) {
		Serializable save = this.sessionFactory.getCurrentSession().save(knowledge);
		if(save!=null){
			return true;
		}
		return false;
	}

	@Override
	public void delUser(User user) {
		this.sessionFactory.getCurrentSession().delete(user);
		
	}

	@Override
	public void delDoctor(Doctor doctor) {
		this.sessionFactory.getCurrentSession().delete(doctor);
		
	}

	@Override
	public void delKnowledge(Knowledge knowledge) {
		this.sessionFactory.getCurrentSession().delete(knowledge);
		
	}

	@Override
	public int selCountUser() {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("select count(*) from User");
			int count=((Long) query.setCacheable(true).uniqueResult()).intValue();
			return count;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
//		Query query = this.sessionFactory.getCurrentSession().createQuery("select count(*) from CookBook");
//		int count=((Long) query.setCacheable(true).uniqueResult()).intValue();
//		return count;
	}

	@Override
	public int selCountDoctor() {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("select count(*) from Doctor");
			int count=((Long) query.setCacheable(true).uniqueResult()).intValue();
			return count;
		} catch (Exception e) {
			// TODO: handle exception
		}	
		return 0;
	}

	@Override
	public int selCountKnowledge() {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("select count(*) from Knowledge");
			int count=((Long) query.setCacheable(true).uniqueResult()).intValue();
			return count;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}
	
	@Override
	public List<User> selAllUser(int pageNum, int pageSize) {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("from User");
			query.setFirstResult((pageNum-1)*pageSize);
			query.setMaxResults(pageSize);
			List<User> user = query.list();
			return user;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
//		Query query = this.sessionFactory.getCurrentSession().createQuery("from User");
//		query.setFirstResult((pageNum-1)*pageSize);
//		query.setMaxResults(pageSize);
//		List<User> user = query.list();
//		return user;
	}

	@Override
	public List<Doctor> selAllDoctor(int pageNum, int pageSize) {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("from Doctor");
			query.setFirstResult((pageNum-1)*pageSize);
			query.setMaxResults(pageSize);
			List<Doctor> doctor = query.list();
			return doctor;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public List<Knowledge> selAllKnowledge(int pageNum, int pageSize) {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("from Knowledge");
			query.setFirstResult((pageNum-1)*pageSize);
			query.setMaxResults(pageSize);
			List<Knowledge> knowledge = query.list();
			return knowledge;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	@Override
	public boolean updUser(String name, String password, String img, String sex, double money, String email) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("update User set password=?,img=?,sex=?,money=?,email=? where name=?");
		query.setParameter(0, password);
		query.setParameter(1, img);
		query.setParameter(2, sex);
		query.setParameter(3, money);
		query.setParameter(4, email);
		query.setParameter(5, name);
		int executeUpdate = query.executeUpdate();
		if(executeUpdate==1){
			return true;
		}
		return false;
	}

	@Override
	public boolean updDoctor(String name, String password, String sex, String information, double money, String section,
			String email) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("update Doctor set password=?,sex=?,information=?,money=?,section=?,email=? where name=?");
		query.setParameter(0, password);
		query.setParameter(1, sex);
		query.setParameter(2, information);
		query.setParameter(3, money);
		query.setParameter(4, section);
		query.setParameter(5, email);
		query.setParameter(6, name);
		int executeUpdate = query.executeUpdate();
		if(executeUpdate==1){
			return true;
		}
		return false;
	}

	@Override
	public boolean updKnowledge(String name, String title, String content, Doctor doctor) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("update Knowledge set title=?,content=?,doctor=? where name=?");
		query.setParameter(0, title);
		query.setParameter(1, content);
		query.setParameter(2, doctor);
		query.setParameter(3, name);
		int executeUpdate = query.executeUpdate();
		if(executeUpdate==1){
			return true;
		}
		return false;
	}

	
	
}
